//
// Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#include <Atomic/IO/File.h>
#include <Atomic/IO/FileSystem.h>
#include <Atomic/Core/StringUtils.h>

#include "../JSBind.h"
#include "../JSBModule.h"
#include "../JSBPackage.h"
#include "../JSBEnum.h"
#include "../JSBClass.h"

#include "../JSBDoc.h"
#include "../JSBTypeScript.h"
#include "../JSBHaxe.h"

#include "JSPackageWriter.h"
#include "JSModuleWriter.h"

namespace ToolCore
{

JSPackageWriter::JSPackageWriter(JSBPackage *package) : JSBPackageWriter(package)
{

}

void JSPackageWriter::WriteProtoTypeRecursive(String &source, JSBClass* klass,  Vector<JSBClass*>& written)
{
    if (written.Contains(klass))
        return;

    if (klass->GetModule()->GetDotNetModule())
        return;

    PODVector<JSBClass*>& baseClasses = klass->GetBaseClasses();

    Vector<JSBClass*>::Iterator itr = baseClasses.End() - 1 ;

    while (itr != baseClasses.Begin() - 1)
    {
        WriteProtoTypeRecursive(source, (*itr), written);
        itr--;
    }

    JSBClass* base = baseClasses.Size() ? baseClasses[0] : NULL;

    if (!klass->IsNumberArray() && klass->GetPackage() == package_)
    {
        JSBModule* module = klass->GetModule();

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        if (module->Requires("3D"))
            source += "\n#ifdef ATOMIC_3D\n";

        String packageName =  klass->GetModule()->GetPackage()->GetName();
        String basePackage =  base ? base->GetModule()->GetPackage()->GetName() : "";

        source.AppendWithFormat("   js_setup_prototype(vm, \"%s\", \"%s\", \"%s\", \"%s\", %s);\n",
                                packageName.CString(), klass->GetName().CString(),
                                base ? basePackage.CString() : "", base ? base->GetName().CString() : "",
                                klass->HasProperties() ? "true" : "false");

        if (module->Requires("3D"))
            source += "#endif\n\n";

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    written.Push(klass);

}

void JSPackageWriter::WriteProtoTypeSetup(String& source)
{
    Vector<JSBClass*> written;

    PODVector<JSBClass*> allClasses = package_->GetAllClasses();

    for (unsigned i = 0; i < allClasses.Size(); i++)
    {
        WriteProtoTypeRecursive(source, allClasses[i], written);
    }
}

void JSPackageWriter::GenerateSource()
{
    String source = "// This file was autogenerated by JSBind, changes will be lost\n\n";

    String defineGuard = package_->GetPlatformDefineGuard();

    if (defineGuard.Length())
    {
        source += ToString("%s\n\n", defineGuard.CString());        
    }

    source += "#include <Duktape/duktape.h>\n";
    source += "#include <Atomic/Script/ScriptVector.h>\n";
    source += "#include <AtomicJS/Javascript/JSVM.h>\n";
    source += "#include <AtomicJS/Javascript/JSAPI.h>\n";

    source += "\n\nnamespace Atomic\n{\n";

    String packageLower = package_->GetName().ToLower();

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        JSBModule* module = package_->modules_.At(i);

        if (module->GetDotNetModule())
            continue;

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        String moduleLower = module->GetName().ToLower();

        source.AppendWithFormat("\nextern void jsb_package_%s_preinit_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());
        source.AppendWithFormat("\nextern void jsb_package_%s_init_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    source += "\n\nstatic void jsb_modules_setup_prototypes(JSVM* vm)\n{\n";

    source += "   // It is important that these are in order so the prototypes are created properly\n";
    source += "   // This isn't trivial as modules can have dependencies, so do it here\n\n";

    WriteProtoTypeSetup(source);

    source += "\n}\n";

    source.AppendWithFormat("\n\nstatic void jsb_package_%s_preinit(JSVM* vm)\n{", packageLower.CString());


    source.Append("\n    // Create the global package object\n");
    source.Append("    duk_context* ctx = vm->GetJSContext();\n");
    source.Append("    duk_push_object(ctx);\n");
    source.AppendWithFormat("    duk_put_global_string(ctx, \"%s\");\n", package_->GetName().CString());

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        JSBModule* module = package_->modules_.At(i);

        if (module->GetDotNetModule())
            continue;

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        if (module->Requires("3D"))
            source += "\n#ifdef ATOMIC_3D";

        String moduleLower = module->GetName().ToLower();

        source.AppendWithFormat("\n   jsb_package_%s_preinit_%s(vm);", packageLower.CString(), moduleLower.CString());

        if (module->Requires("3D"))
            source += "\n#endif //ATOMIC_3D\n";

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    source += "\n}\n\n";

    source.AppendWithFormat("\n\nvoid jsb_package_%s_init(JSVM* vm)\n{", packageLower.CString());

    source.AppendWithFormat("\n\n   jsb_package_%s_preinit(vm);\n", packageLower.CString());

    source += "\n\n   jsb_modules_setup_prototypes(vm);\n";

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        JSBModule* module = package_->modules_.At(i);

        if (module->GetDotNetModule())
            continue;

        String moduleLower = module->GetName().ToLower();

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        if (module->Requires("3D"))
            source += "\n#ifdef ATOMIC_3D";

        source.AppendWithFormat("\n   jsb_package_%s_init_%s(vm);", packageLower.CString(), moduleLower.CString());
        
        if (module->Requires("3D"))
        {
            source += "\n#endif //ATOMIC_3D\n";
        }

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    source += "\n}\n\n";

    // end Atomic namespace
    source += "\n}\n";

    if (defineGuard.Length())
    {
        source += "\n#endif\n";
    }

    JSBind* jsbind = package_->GetSubsystem<JSBind>();

    String filepath = jsbind->GetDestNativeFolder() + "/JSPackage" + package_->name_ + ".cpp";

    File file(package_->GetContext());
    file.Open(filepath, FILE_WRITE);
    file.Write(source.CString(), source.Length());
    file.Close();

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        if (package_->modules_[i]->GetDotNetModule())
            continue;

        JSModuleWriter writer(package_->modules_[i]);
        writer.GenerateSource();
    }

}

void JSPackageWriter::PostProcess()
{

    JSBind* jsbind = package_->GetSubsystem<JSBind>();

    JSBDoc jdoc;
    jdoc.Emit(package_, jsbind->GetSourceRootFolder() + "Artifacts/Build/JSDoc/" + package_->GetName() + ".js");

    JSBTypeScript ts;
    ts.Emit(package_, jsbind->GetSourceRootFolder() + "Script/TypeScript/" + package_->GetName() + ".d.ts");

    JSBHaxe hx;
    hx.Emit(package_, jsbind->GetSourceRootFolder() + "Script/Haxe/" + package_->GetName() + ".hx");

}

}
